Ext.onReady(function(){
    Ext.define('json_data',{
        extend: 'Ext.data.Model',
        fields: [
			 {name:'id',type:'string'},
//             {name:'birthday',type:'y年m月d日'},
             {name:'a',type:'string'},
             {name:'b',type:'string'},
             {name:'c',type:'string'},
             {name:'d',type:'string'},
             {name:'e',type:'string'},
             {name:'f',type:'string'},
             {name:'g',type:'string'},
             {name:'h',type:'string'},
             {name:'i',type:'string'},
             {name:'j',type:'string'},
             {name:'k',type:'string'},
             {name:'l',type:'string'},
             {name:'m',type:'string'},
             {name:'n',type:'string'},
             {name:'o',type:'string'},
             {name:'p',type:'string'},
             {name:'q',type:'string'},
             {name:'r',type:'string'},
             {name:'s',type:'string'},
             {name:'t_id',type:'string'},
             {name:'t__name',type:'string'},
             {name:'u',type:'string'},
             {name:'v',type:'string'},
             {name:'w',type:'string'},
             
             
             
             // 资源学生回访表字段
             {name:'at_time',type:'string'},
             {name:'student_resource_id',type:'string'},
             {name:'at_time',type:'string'},
             {name:'content',type:'string'},
             {name:'remark',type:'string'}
        ]
    });
    //创建数据源
    var store = Ext.create('Ext.data.Store', {
    	pageSize:20,
        model: 'json_data',
	    proxy: {
	        type: 'ajax',
	        url: '/customercontrol/student_resource_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    autoLoad: false
    });
    //创建数据源
    var storeVisit = Ext.create('Ext.data.Store', {
    	pageSize:20,
        model: 'json_data',
	    proxy: {
	        type: 'ajax',
	        url: '/customercontrol/student_resource_visit_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    autoLoad: true
    });
    
  	//年级combox
    var gradeComboBox= Ext.create('Ext.form.field.ComboBox', { 
	   name:'grade_id',
       fieldLabel: '年级', 
       store: new Ext.data.Store({ 
           singleton : true, 
	   	   proxy: {
	   	        type: 'ajax',
	   	        url: '/customercontrol/grade_select/',
	   	        actionMethods:'post',
	   	        reader: {
	   	            type: 'json'
	   	        }
	   	    },
           fields:['id', 'name'],
           autoLoad:true 
       }), 
       loadingText:'正在加载数据，请稍侯……', 
       disabled:true,
       triggerAction:'all', 
       valueField:'id', 
       displayField:'name'
    }); 
  	//公司combox
    var companyComboBox= Ext.create('Ext.form.field.ComboBox', {
        id:'company_id1',
	   	name:'company_id',
       	fieldLabel: '校区', 
       	emptyText: '请选择',
       	store: new Ext.data.Store({ 
           	singleton : true, 
	   	    proxy: {
	   	        type: 'ajax',
	   	        url: '/resourcecontrol/company_select/',
	   	        actionMethods:'post',
	   	        reader: {
	   	            type: 'json'
	   	        }
	   	    },
           fields:['id', 'cn_name'], 
           autoLoad:true 
       	}), 
       	loadingText:'正在加载数据，请稍侯……', 
       	triggerAction:'all', 
       	disabled:true,
       	valueField:'id', 
       	displayField:'cn_name'
    });

    
    //状态combox
    var recordStateComboBox= Ext.create('Ext.form.field.ComboBox', { 
    	id:'deal_state_combobox_id',
    	name:'deal_state',
    	emptyText: '请选择状态',
    	labelWidth: 60,
    	width : 180,
    	fieldLabel: '处理状态', 
    	editable:false,
    	store: new Ext.data.Store({ 
			fields:['id','value'], 
			data:[
			    {'id':'' ,'value':'全部'}, 
			    {'id':'0' ,'value':'未处理'}, 
				{'id':'1','value':'建议跟踪'},
				{'id':'2','value':'强烈建议跟踪'},
				{'id':'3','value':'有意向'},
				{'id':'4','value':'放弃'}
			],
	    	autoLoad:false 
    	}), 
    	loadingText:'正在加载数据，请稍侯……', 
    	triggerAction:'all', 
    	valueField:'id', 
    	displayField:'value'
    }); 
    
    //创建Grid
    var grid = Ext.create('Ext.grid.Panel',{
        store: store,
        //height: 500,
        //selModel: Ext.create('Ext.selection.CheckboxModel'),
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列
			{text: "ID", width: 100, dataIndex: 'id', sortable: false,hidden : true},
			{text: "学生姓名", width: 100, dataIndex: 'd', sortable: false},
			{text: "家长姓名", width: 100, dataIndex: 'p', sortable: false},
			{text: "联系电话", width: 100, dataIndex: 'q', sortable: false},
			{text: "班级", width: 100, dataIndex: 'a', sortable: false},
			{text: "家庭地址", width: 100, dataIndex: 'n', sortable: false},
			{text: "处理状态", width: 100, dataIndex: 'w', sortable: false,renderer:function(v){
	            	if (v==0) 
	                	return '未处理'  
	            	if (v==1) 
	                	return '建议跟踪'
	            	if (v==2) 
	                	return '强烈建议跟踪'
	            	if (v==3) 
	                	return '有意向' 
	    			else
	        			return '放弃'
            	}
            },
			{text: "学号", width: 100, dataIndex: 'b', sortable: false,hidden : true},
			{text: "学籍号", width: 100, dataIndex: 'c', sortable: false,hidden : true},
			{text: "曾用名", width: 100, dataIndex: 'e', sortable: false},
			{text: "性别", width: 100, dataIndex: 'f', sortable: false},
			{text: "出生日期", width: 100, dataIndex: 'g', sortable: false},
			{text: "身份证号", width: 100, dataIndex: 'h', sortable: false,hidden : true},
			{text: "政治面貌", width: 100, dataIndex: 'i', sortable: false,hidden : true},
			{text: "民族", width: 100, dataIndex: 'j', sortable: false},
			{text: "籍贯", width: 100, dataIndex: 'k', sortable: false},
			{text: "学生来源", width: 100, dataIndex: 'l', sortable: false,hidden : true},
			{text: "户口所在地", width: 100, dataIndex: 'm', sortable: false,hidden : true},
			{text: "户口所在乡镇", width: 100, dataIndex: 'o', sortable: false,hidden : true},
			{text: "数据提供人", width: 100, dataIndex: 'r', sortable: false},
			{text: "数据来源", width: 100, dataIndex: 's', sortable: false},
			{text: "电邀师ID", width: 100, dataIndex: 't_id', sortable: false,hidden : true},
			{text: "电邀师", width: 100, dataIndex: 't__name', sortable: false},
			{text: "备注", width: 100, dataIndex: 'u', sortable: false},
			{text: "数据状态", width: 100, dataIndex: 'v', sortable: false,renderer:function(v){
	            	if (v==1) 
	                	return '有效'  
	    			else
	        			return '无效'
            	}
            ,hidden : true}
			
        ],
        title: '陌拜资源',
        // disableSelection: false,
        layout : 'fit',
        //frame:true,
        loadMask:true,
//        region: 'north',
//        renderTo: 'customer-tab',
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
    				gridVisit.enable() ;
                },
                itemdblclick: function(dataview, record, item, index, e) {
//                	var selected = grid.getSelectionModel().getSelection();
//    				form.getForm().reset();
//					form.loadRecord(selected[0]);
//        			win.show();
                }
            }
        },
        dockedItems : [{
            xtype : 'pagingtoolbar',
            store : store,
            dock : 'bottom',
            displayInfo : true,
            //displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
            autoScroll:true 
        },{
        	dock:'top',
        	xtype:'toolbar',
        	items:[/*{
        		xtype: 'combobox',
        		labelWidth: 30,
        		width:130,
        		id:'company_combox_id',
            	name:'company',
               	fieldLabel: '校区',
               	emptyText: '请选择校区',
               	editable:false,
               	store: new Ext.data.Store({
                   	singleton : true, 
        	   	    proxy: {
        	   	        type: 'ajax',
        	   	        url: '/resourcecontrol/company_select/',
        	   	        actionMethods:'post',
        	   	        reader: {
        	   	            type: 'json'
        	   	        }
        	   	    },
                   fields:['id', 'cn_name'], 
                   autoLoad:true 
               	}), 
               	loadingText:'正在加载数据，请稍侯……', 
               	triggerAction:'all', 
               	valueField:'id', 
               	displayField:'cn_name'        		
        	},*/{
        		id:'name',
        		xtype:'textfield',
        		editable:true,
        		labelWidth: 30,
        		width:130,
        		fieldLabel:'姓名'
        	},recordStateComboBox,{
        		xtype:'button',
        		text:'查询',
        		iconCls:'search', 
        		handler:function(){
//        			if(Ext.getCmp('company_combox_id').getValue()!=null){
//	        			var params = {name: Ext.getCmp('name').getValue(),
//	        			company_id: Ext.getCmp('company_combox_id').getValue(),
//	        			deal_state:Ext.getCmp('deal_state_combobox_id').getValue()};
//	        		    Ext.apply(store.proxy.extraParams, params);
//	        		    store.currentPage=1;
//	        		    store.load();
//        			}else{
//        				Ext.Msg.alert('系统提示', '请选择校区!');
//        			}
        			var params = {name: Ext.getCmp('name').getValue(),
        			deal_state:Ext.getCmp('deal_state_combobox_id').getValue()};
        		    Ext.apply(store.proxy.extraParams, params);
        		    store.currentPage=1;
        		    store.load();
        		}
        	},/*{
        		xtype:'button',
        		text:'详情',
        		iconCls:'edit', 
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				form.getForm().reset();
						form.loadRecord(selected[0]);
		    			win.show();
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要查看详情的学生资源!');
	    			}
        		}
        	},*/{
        		xtype:'button',
        		text:'修改',
        		iconCls:'edit', 
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				editForm.getForm().reset();
						editForm.loadRecord(selected[0]);
		    			editWin.show();
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要修改的学生资源!');
	    			}
        		}
        	}]
        }]
    });
    
    
    var form = Ext.create('Ext.form.Panel', {
		width: 550,
		//frame: true,
		bodyPadding: 5,
        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'anchor',
        defaults: {
            anchor: '100%'
        },
		items : [{
			xtype:'hiddenfield',
			fieldLabel:'id',
			name:'id'
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '姓名',
				name : 'name',
				disabled:true,
				allowBlank : false,
				blankText : '请输入姓名'
			}, {
				xtype:'combobox',
				editable:false,
				disabled:true,
				valueField:'id',
				displayField:'value',
				queryMode:'local',
				fieldLabel : '性别',
				name : 'sex',
				allowBlank : false,
				blankText : '请输入性别',
				store:Ext.create('Ext.data.Store',{
					fields:['id','value'], 
					data:[
						{'id':'1','value':'男'},
						{'id':'2','value':'女'}
					]
				}),
				columnWidth: 1/2
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'datefield',
			items:[ {
				fieldLabel : '出生日期',
				name : 'birthday',
				disabled:true,
				allowBlank : false,
				format: 'Y-m-d',
				blankText : '请输入'
			}, {
				fieldLabel : '学校名称',
				xtype: 'textfield',
				disabled:true,
				name : 'school', 
				allowBlank : true,
				blankText : '请输入'
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ /*{
				fieldLabel : '年级',
				name : 'grade',
				allowBlank : true,
				blankText : '请输入'
			}*/gradeComboBox, {
				fieldLabel : '联系电话',
				xtype: 'textfield',
				disabled:true,
				name : 'rela_mobile', 
				allowBlank : true,
				blankText : '请输入'
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '导入时间',
				xtype: 'textfield',
				name : 'load_time', 
				disabled:true,
				allowBlank : true,
				blankText : '请输入'
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				xtype:'combobox',
				editable:false,
				valueField:'id',
				displayField:'value',
				queryMode:'local',
				fieldLabel : '处理状态',
				disabled:true,
				name : 'deal_state',
				allowBlank : false,
				blankText : '请输入',
				store:Ext.create('Ext.data.Store',{
					fields:['id','value'], 
					//状态(0未处理  1建议跟踪 2强烈建议跟踪 3有意向 4放弃)
					data:[
					    {'id':'0','value':'未处理'},
						{'id':'1','value':'建议跟踪'},
						{'id':'2','value':'强烈建议跟踪'},
						{'id':'3','value':'有意向'},
						{'id':'4','value':'放弃'}
					]
				}),
				columnWidth: 1/2
			},companyComboBox]
		},{
			xtype:'textfield',
			fieldLabel:'家庭地址',
			disabled:true,
			name:'guardian_addr'
		},{
			xtype:'textareafield',
			fieldLabel:'备注',
			disabled:true,
			name:'remark'
		}]
	});

	
	var win = Ext.create('Ext.window.Window', {
		title : '资源学生信息',
		modal: true,
		closeAction: 'hide',
		items : [form],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [{
			text : '确定',
			handler : function() {
				form.getForm().reset();
				win.hide();
			}
		} ]
	});
    

	
	// 修改资源学生状态
	var editForm = Ext.create('Ext.form.Panel', {
		width: 550,
		//frame: true,
		bodyPadding: 5,
        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'anchor',
        defaults: {
            anchor: '100%'
        },
		items : [{
			xtype:'hiddenfield',
			fieldLabel:'id',
			name:'id'
		},{
			xtype:'textfield',
			fieldLabel:'姓名',
			disabled:true,
			name:'d'
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				xtype:'combobox',
				editable:false,
				valueField:'id',
				displayField:'value',
				queryMode:'local',
				fieldLabel : '处理状态',
				name : 'w',
				allowBlank : false,
				blankText : '请输入',
				store:Ext.create('Ext.data.Store',{
					fields:['id','value'], 
					//状态(0未处理  1建议跟踪 2强烈建议跟踪 3有意向 4放弃)
					data:[
					    {'id':'0','value':'未处理'},
						{'id':'1','value':'建议跟踪'},
						{'id':'2','value':'强烈建议跟踪'},
						{'id':'3','value':'有意向'},
						{'id':'4','value':'放弃'}
					]
				}),
				columnWidth: 1/2
			}]
		},{
			xtype:'textareafield',
			fieldLabel:'备注',
			name:'u'
		}]
	});

	
	var editWin = Ext.create('Ext.window.Window', {
		title : '资源学生信息',
		modal: true,
		closeAction: 'hide',
		items : [editForm],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [{
			text : '保存',
			handler : function() {
				if (editForm.getForm().isValid()) {
					editForm.getForm().submit({
						waitTitle : '请稍候',
						waitMsg : '正在处理......',
						url : '/customercontrol/student_resource_update_state/',
						success : function(form, action) {
							editWin.hide();
							store.load();
						},
						failure : function(form, action) {
							if (action.result) {
								Ext.Msg.alert('系统提示', action.result.message);
							} 
						}
					});
				}
			}
		}, {
			text : '取消',
			handler : function() {
				editForm.getForm().reset();
				editWin.hide();
			}
		} ]
	});
    
	
	
    
    
    //--
    //创建学生回访Grid
    var gridVisit = Ext.create('Ext.grid.Panel',{
    	
    	disabled:true,
        store: storeVisit,
        //height: 500,
        //selModel: Ext.create('Ext.selection.CheckboxModel'),
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列
			{text: "ID", width: 100, dataIndex: 'id', sortable: false,hidden : true},
			{text: "资源学生ID", width: 100, dataIndex: 'student_resource_id', sortable: false,hidden : true},
			{text: "回访时间", width: 100, dataIndex: 'at_time', sortable: false},
			{text: "回访内容", width: 300, dataIndex: 'content', sortable: false},
            {text: "备注", width: 300, dataIndex: 'remark', sortable: false}
        ],
        title: '回访信息',
        // disableSelection: false,
        layout : 'fit',
        //frame:true,
        loadMask:true,
//        region: 'north',
//        renderTo: 'customer-tab',
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
					
                },
                itemdblclick: function(dataview, record, item, index, e) {
	    			formVisit.getForm().reset();
        			var selected = gridVisit.getSelectionModel().getSelection();
					formVisit.loadRecord(selected[0]);
					// 设置学生ID
        			var selected = grid.getSelectionModel().getSelection();
        			var ids="";
        			Ext.each(selected, function (item) {
        				ids+=item.data.id;
        			});
        			Ext.getCmp('formVisit').getForm().setValues({student_resource_id:ids});
        			winVisit.show();
                }
            }
        },
        dockedItems : [{
            xtype : 'pagingtoolbar',
            store : store,
            dock : 'bottom',
            displayInfo : true,
            //displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
            autoScroll:true 
        },{
        	dock:'top',
        	xtype:'toolbar',
        	items:[/*{
        		xtype:'button',
        		text:'测试-需要修改为TAB激活',
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
        			var ids="";
        			Ext.each(selected, function (item) {
        				ids+=item.data.id;
        			});
        			//alert(ids) ;
        			Ext.getCmp('formVisit').getForm().setValues({student_id:ids});
        			
        			var params = {student_id: ids};
        		    Ext.apply(storeVisit.proxy.extraParams, params);
        		    storeVisit.load();
        		}
        	},*/{
        		xtype:'button',
        		text:'添加',
        		iconCls:'add', 
        		handler:function(){
        			formVisit.getForm().reset();
        			// 设置学生ID
        			var selected = grid.getSelectionModel().getSelection();
        			var ids="";
        			Ext.each(selected, function (item) {
        				ids+=item.data.id;
        			});
        			//alert(ids) ;
        			Ext.getCmp('formVisit').getForm().setValues({student_resource_id:ids});
        			winVisit.show();
        		}
        	},{
        		xtype:'button',
        		text:'修改',
        		iconCls:'edit', 
        		handler:function(){
        			var selected = gridVisit.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				formVisit.getForm().reset();
	        			var selected = gridVisit.getSelectionModel().getSelection();
						formVisit.loadRecord(selected[0]);
						var selected = grid.getSelectionModel().getSelection();
	        			var ids="";
	        			Ext.each(selected, function (item) {
	        				ids+=item.data.id;
	        			});
	        			Ext.getCmp('formVisit').getForm().setValues({student_resource_id:ids});
	        			winVisit.show();
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要修改的回访记录!');
	    			}
        		
        			
        		}
        	},{
        		xtype:'button',
        		text:'删除',
        		iconCls:'delete', 
        		handler:function(){
        			var selected = gridVisit.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				Ext.MessageBox.confirm('提示', '确认删除吗?', function(btn){
	        				if(btn=='yes'){
	        					var ids="";
	                			Ext.each(selected, function (item) {
	                				ids+=item.data.id+",";
	                			});
	                			Ext.Ajax.request({
	                			    url:'/customercontrol/student_resource_visit_del/',
	                			    method : 'post',
	                			    params:{
	                			    	ids:ids
	                			    },
	                			    success:function(response){
	                			        var results =  Ext.decode(response.responseText);
	                			        if(results.success){
	                			        	storeVisit.load();
	                			        }else{
	                			        	Ext.Msg.alert('系统提示', results.message);
	                			        }
	                			    }
	                			});
	        				}
	        			});
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要删除的回访记录!');
	    			}
        			
        		}
        	}]
        }]
    });
    
	var formVisit = Ext.create('Ext.form.Panel', {
		id:'formVisit',
		width: 350,
		//frame: true,
		bodyPadding: 5,
        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'anchor',
        defaults: {
            anchor: '100%'
        },
		items : [{
			xtype:'hiddenfield',
			fieldLabel:'ID',
			name:'id'
		},{
			xtype:'hiddenfield',
			fieldLabel:'资源学生ID',
			name:'student_resource_id'
		},{
			xtype:'datefield',
			fieldLabel:'回访时间',
			allowBlank : false,
			format: 'Y-m-d',
			name:'at_time'
		},{
			xtype:'textareafield',
			fieldLabel:'回访内容',
			allowBlank : false,
			name:'content'
		},{
			xtype:'textareafield',
			fieldLabel:'备注',
			allowBlank : true,
			name:'remark'
		}]
	});

	
	var winVisit = Ext.create('Ext.window.Window', {
		title : '回访信息',
		modal: true,
		closeAction: 'hide',
		items : [formVisit],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [ {
			text : '保存',
			handler : function() {
				if (formVisit.getForm().isValid()) {
					formVisit.getForm().submit({
						waitTitle : '请稍候',
						waitMsg : '正在处理......',
						url : '/customercontrol/student_resource_visit_save/',
						success : function(form, action) {
							winVisit.hide();
							storeVisit.load();
						},
						failure : function(form, action) {
							if (action.result) {
								Ext.Msg.alert('系统提示', action.result.message);
							} 
						}
					});
				}
			}
		}, {
			text : '取消',
			handler : function() {
				formVisit.getForm().reset();
				winVisit.hide();
			}
		} ]
	});

	

	

	
	Ext.create('Ext.container.Viewport',{
		layout:'fit',
		renderTo: 'customer-resource-tab',
		items:[Ext.create('Ext.tab.Panel',{
			//renderTo: 'customer-resource-tab',
			activeTab: 0,
			items:[grid,gridVisit],
			listeners:{
				'tabchange':function(tabPanel, newCard,oldCard,eOpts){
					if(newCard==gridVisit){
						var selected = grid.getSelectionModel().getSelection();
	        			var ids="";
	        			Ext.each(selected, function (item) {
	        				ids+=item.data.id;
	        			});
	        			Ext.getCmp('formVisit').getForm().setValues({student_resource_id:ids});
	        			var params = {student_resource_id: ids};
	        		    Ext.apply(storeVisit.proxy.extraParams, params);
	        		    storeVisit.load();
					}else{// 主grid
						gridVisit.disable() ;
						gridTrack.disable() ;
					}
				}
			}
		})]
	});
	
});